import http from "@/api";

// 物料群组相关接口
export namespace MaterialGroup {
  // 创建物料群组请求参数
  export interface CreateMaterialGroupRequest {
    groupCode: string;
    groupName: string;
    description?: string;
    status?: string;
    groupType?: string;
  }

  // 更新物料群组请求参数
  export interface UpdateMaterialGroupRequest {
    groupName: string;
    description?: string;
    status?: string;
    groupType?: string;
  }

  // 物料群组列表项
  export interface MaterialGroupListItem {
    id: string;
    groupCode: string;
    groupName: string;
    description?: string;
    status: string;
    groupType?: string;
    materialCount?: number;
    createdBy?: string;
    createdTime: string;
    updatedBy?: string;
    updatedTime: string;
  }

  // 物料群组列表响应（MyBatis-Plus 分页结构）
  export interface MaterialGroupListResponse {
    records: MaterialGroupListItem[];
    total: number;
    size: number;
    current: number;
    pages: number;
  }
}

/** 创建物料群组 */
export const createMaterialGroup = (params: MaterialGroup.CreateMaterialGroupRequest) => {
  return http.post<Long>("/materialGroups/create", params);
};

/** 获取物料群组列表 */
export const getMaterialGroupList = (params?: {
  groupCode?: string;
  groupName?: string;
  status?: string;
  groupType?: string;
  parentGroupId?: string;
  current?: number;
  size?: number;
}) => {
  return http.get<MaterialGroup.MaterialGroupListResponse>("/materialGroups/list", params);
};

/** 更新物料群组 */
export const updateMaterialGroup = (id: string, params: MaterialGroup.UpdateMaterialGroupRequest) => {
  return http.put<void>(`/materialGroups/${id}`, params);
};

/** 删除物料群组 */
export const deleteMaterialGroup = (id: string) => {
  return http.delete<void>(`/materialGroups/${id}`);
};

/** 批量删除物料群组 */
export const batchDeleteMaterialGroup = (ids: string[]) => {
  return http.delete<void>("/materialGroups/batch", { data: { ids } });
};

/** 获取物料群组详情 */
export const getMaterialGroupById = (id: string) => {
  return http.get<MaterialGroup.MaterialGroupListItem>(`/materialGroups/${id}`);
};

/** 启用物料群组 */
export const enableMaterialGroup = (id: string) => {
  return http.put<void>(`/materialGroups/${id}/enable`);
};

/** 禁用物料群组 */
export const disableMaterialGroup = (id: string) => {
  return http.put<void>(`/materialGroups/${id}/disable`);
};

/** 根据群组类型获取群组列表 */
export const getMaterialGroupsByType = (groupType: string) => {
  return http.get<MaterialGroup.MaterialGroupListItem[]>(`/materialGroups/type/${groupType}`);
};

/** 获取启用状态的群组列表 */
export const getActiveMaterialGroups = () => {
  return http.get<MaterialGroup.MaterialGroupListItem[]>("/materialGroups/active");
};